package com.pig4cloud.pig.module.controller.devices;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.DateUtils;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.devices.DevicesEggGetRecord;
import com.pig4cloud.pig.module.api.entity.excel.DevicesGetRecordExcel;
import com.pig4cloud.pig.module.service.devices.IDevicesEggGetRecordService;
import com.vdurmont.emoji.EmojiParser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 鸡蛋柜领取记录Controller
 *
 * @author ruoyi
 * @date 2022-01-06
 */
@Validated
@Api(value = "鸡蛋柜领取记录控制器", tags = {"鸡蛋柜领取记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/devices/getRecord")
public class DevicesEggGetRecordController {

    private final IDevicesEggGetRecordService devicesEggGetRecordService;

    /**
     * 查询鸡蛋柜领取记录列表
     */
    @ApiOperation("查询鸡蛋柜领取记录列表")
    @PreAuthorize("@pms.hasPermission('module:eggGetRecord::list')")
    @GetMapping("/list")
    public TableDataInfo<DevicesEggGetRecord> list(@Validated DevicesEggGetRecord devicesEggGetRecord) {
        QueryWrapper<DevicesEggGetRecord> qw = new QueryWrapper<>();
		Map<String,Object> map = new HashMap<>();

		if(devicesEggGetRecord.getDeviceId() !=null){
			qw.eq("device_id",devicesEggGetRecord.getDeviceId());
			map.put("device_id", devicesEggGetRecord.getDeviceId());
		}
		if(StrUtil.isNotEmpty(devicesEggGetRecord.getBeginDate())){
			qw.between("create_time",devicesEggGetRecord.getBeginDate() + " 00:00:00",devicesEggGetRecord.getEndDate() + " 23:59:59");
			map.put("begin_time", devicesEggGetRecord.getBeginDate() + " 00:00:00");
			map.put("end_time", devicesEggGetRecord.getEndDate() + " 23:59:59");
		}
        TableDataInfo<DevicesEggGetRecord> td = PageUtils.buildDataInfo(devicesEggGetRecordService.selectCellRecordList(PageUtils.buildPage(), map));
        List<DevicesEggGetRecord> list = td.getData();
		if(CollUtil.isNotEmpty(list)){
			for (DevicesEggGetRecord eggGetRecord : list){
				if(StrUtil.isNotEmpty(eggGetRecord.getNickName())){
					eggGetRecord.setNickName(EmojiParser.parseToUnicode(eggGetRecord.getNickName()));
				}
				if(eggGetRecord.getCellType() == 0){
					eggGetRecord.setCellTypeName("鸡蛋");
				} else if(eggGetRecord.getCellType() == 1){
					eggGetRecord.setCellTypeName("抽纸");
				} else if(eggGetRecord.getCellType() == 2){
					eggGetRecord.setCellTypeName("香皂");
				} else if(eggGetRecord.getCellType() == 3){
					eggGetRecord.setCellTypeName("洗衣粉");
				}
			}
		}
        return td;
    }
	/**
	 * 查询鸡蛋换抽纸的数量
	 */
	public Integer eggForPaper(Integer id){
		QueryWrapper<DevicesEggGetRecord> qw=new QueryWrapper<>();
		qw.eq("user_id",id);
		List<DevicesEggGetRecord> list=devicesEggGetRecordService.list(qw);
		Integer eggNub=0;
		for(DevicesEggGetRecord nub1:list){
			if(nub1.getCellType()==1){
				eggNub+=2;
			}
			if(nub1.getCellType()==0){
				eggNub+=4;
			}
		}
		return eggNub;
	}

	/**
	 * 查询鸡蛋柜领取记录列表
	 */
	@ApiOperation("查询鸡蛋柜领取记录列表")
	@PreAuthorize("@pms.hasPermission('module:eggGetRecord::list')")
	@GetMapping("/tongji/list")
	public TableDataInfo<DevicesGetRecordExcel> tongjiList(@Validated DevicesEggGetRecord devicesEggGetRecord) {
		Map<String,Object> map = new HashMap<>();

		if(devicesEggGetRecord.getDeviceId() !=null){
			map.put("device_id", devicesEggGetRecord.getDeviceId());
		}
		if(StrUtil.isNotEmpty(devicesEggGetRecord.getBeginDate())){
			map.put("begin_time", devicesEggGetRecord.getBeginDate() + " 00:00:00");
			map.put("end_time", devicesEggGetRecord.getEndDate() + " 23:59:59");
		}
		TableDataInfo<DevicesGetRecordExcel> td = PageUtils.buildDataInfo(devicesEggGetRecordService.selectTongjiByDeviceId(PageUtils.buildPage(), map));
		return td;
	}

	/**
	 * 导出鸡蛋柜领取记录列表
	 */
	@ApiOperation("导出鸡蛋柜领取记录列表")
	@PreAuthorize("@pms.hasPermission('module:eggGetRecord::export')")
	@SysLog("鸡蛋柜领取记录")
	@GetMapping("/exportTotal")
	public List exportTotal(@Validated DevicesEggGetRecord devicesEggGetRecord) {
		Map<String,Object> map = new HashMap<>();

		if(devicesEggGetRecord.getDeviceId() !=null){
			map.put("device_id", devicesEggGetRecord.getDeviceId());
		}
		if(StrUtil.isNotEmpty(devicesEggGetRecord.getBeginDate())){
			map.put("begin_time", devicesEggGetRecord.getBeginDate() + " 00:00:00");
			map.put("end_time", devicesEggGetRecord.getEndDate() + " 23:59:59");
		}
		List<DevicesGetRecordExcel> list = devicesEggGetRecordService.selectTongjiByDeviceId(map);
		// ExcelUtil<DevicesGetRecordExcel> util = new // ExcelUtil<DevicesGetRecordExcel>(DevicesGetRecordExcel.class);
		return  list;// util.exportExcel(list, "鸡蛋柜领取记录");
	}


	/**
     * 导出鸡蛋柜领取记录列表
     */
    @ApiOperation("导出鸡蛋柜领取记录列表")
    @PreAuthorize("@pms.hasPermission('module:eggGetRecord::export')")
    @SysLog("鸡蛋柜领取记录")
    @GetMapping("/export")
    public List export(@Validated DevicesEggGetRecord devicesEggGetRecord) {
		QueryWrapper<DevicesEggGetRecord> qw = new QueryWrapper<>();
		Map<String,Object> map = new HashMap<>();

		if(devicesEggGetRecord.getDeviceId() !=null){
			qw.eq("device_id",devicesEggGetRecord.getDeviceId());
			map.put("device_id", devicesEggGetRecord.getDeviceId());
		}
		if(StrUtil.isNotEmpty(devicesEggGetRecord.getBeginDate())){
			qw.between("create_time",devicesEggGetRecord.getBeginDate() + " 00:00:00",devicesEggGetRecord.getEndDate() + " 23:59:59");
			map.put("begin_time", devicesEggGetRecord.getBeginDate() + " 00:00:00");
			map.put("end_time", devicesEggGetRecord.getEndDate() + " 23:59:59");
		}
        List<DevicesEggGetRecord> list = devicesEggGetRecordService.selectCellRecordList(map);
		if(CollUtil.isNotEmpty(list)){
			for (DevicesEggGetRecord eggGetRecord : list){
				eggGetRecord.setCreateTimeStr(DateUtils.getTime(eggGetRecord.getCreateTime()));

				if(StrUtil.isNotEmpty(eggGetRecord.getNickName())){
					eggGetRecord.setNickName(EmojiParser.parseToUnicode(eggGetRecord.getNickName()));
				}
				if(eggGetRecord.getCellType() == 0){
					eggGetRecord.setCellTypeName("鸡蛋");
				} else if(eggGetRecord.getCellType() == 1){
					eggGetRecord.setCellTypeName("抽纸");
				} else if(eggGetRecord.getCellType() == 2){
					eggGetRecord.setCellTypeName("香皂");
				} else if(eggGetRecord.getCellType() == 3){
					eggGetRecord.setCellTypeName("洗衣粉");
				}
			}
		}
        // ExcelUtil<DevicesEggGetRecord> util = new // ExcelUtil<DevicesEggGetRecord>(DevicesEggGetRecord.class);
        return  list;// util.exportExcel(list, "鸡蛋柜领取记录");
    }

    /**
     * 获取鸡蛋柜领取记录详细信息
     */
    @ApiOperation("获取鸡蛋柜领取记录详细信息")
    @PreAuthorize("@pms.hasPermission('module:eggGetRecord::query')")
    @GetMapping("/{id}")
    public R<DevicesEggGetRecord> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        DevicesEggGetRecord devicesEggGetRecord = devicesEggGetRecordService.getById(id);
        return R.ok(devicesEggGetRecord);
    }

    /**
     * 新增鸡蛋柜领取记录
     */
    @ApiOperation("新增鸡蛋柜领取记录")
    @PreAuthorize("@pms.hasPermission('module:eggGetRecord::add')")
    @SysLog("鸡蛋柜领取记录")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody DevicesEggGetRecord devicesEggGetRecord) {
        return R.okByRow(devicesEggGetRecordService.save(devicesEggGetRecord) ? 1 : 0);
    }

    /**
     * 修改鸡蛋柜领取记录
     */
    @ApiOperation("修改鸡蛋柜领取记录")
    @PreAuthorize("@pms.hasPermission('module:eggGetRecord::edit')")
    @SysLog("鸡蛋柜领取记录")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody DevicesEggGetRecord devicesEggGetRecord) {
        return R.okByRow(devicesEggGetRecordService.updateById(devicesEggGetRecord) ? 1 : 0);
    }

    /**
     * 删除鸡蛋柜领取记录
     */
    @ApiOperation("删除鸡蛋柜领取记录")
    @PreAuthorize("@pms.hasPermission('module:eggGetRecord::remove')")
    @SysLog("鸡蛋柜领取记录")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(devicesEggGetRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
